

import 'dart:io';

import 'package:get/get.dart';

import '../models/index.dart';
import '../services/shared_preference_service.dart';
import '../utils/index.dart';

class AppAliFaceDetectionAssembleControlService {
  static AppAliFaceDetectionAssembleControlService get to => Get.find();


  Future<void> checkServiceExist() async {
    final result = await faceConfig();
    if (result != null && result.enable == true ) {
       await SharedPreferenceService.to.putBool(SharedPreferenceService.aliFaceDetectionAppInstallKey, true);
    } else {
       await SharedPreferenceService.to.putBool(SharedPreferenceService.aliFaceDetectionAppInstallKey, false);
    }
  }

   /// 阿里云人脸程序是否已经安装
  bool isInstall() {
    return SharedPreferenceService.to.getBool(SharedPreferenceService.aliFaceDetectionAppInstallKey);
  }

  String baseUrl() {
    return O2ApiManager.instance
            .getModuleBaseUrl(O2DistributeModuleEnum.x_ali_face_detection_assemble_control) ??
        '';
  }

  /// 人脸图片地址
  String faceImageUrl(String fileId) {
    return '${baseUrl()}jaxrs/face/person/download/$fileId';
  }

  /// 配置文件
  Future<FaceConfig?> faceConfig() async {
    try {
      ApiResponse response = await O2HttpClient.instance.get('${baseUrl()}jaxrs/config');
      return FaceConfig.fromJson(response.data);
    } catch (err, stackTrace) {
      OLogger.e('face 配置文件 查询失败', err, stackTrace);
    }
    return null;
  }


  /// 人脸注册
  Future<IMMessageFileResponse?> faceRegistration(File file) async {
    try {
      ApiResponse response = await O2HttpClient.instance.postUploadFile('${baseUrl()}jaxrs/face/person/upload', file);
      return IMMessageFileResponse.fromJson(response.data);
    } catch (err, stackTrace) {
      OLogger.e('faceRegistration 失败', err, stackTrace);
    }
    return null;
  } 
  /// 我的人脸列表
  Future<List<FaceImageData>?> myFaceList() async {
    try {
      ApiResponse response = await O2HttpClient.instance.get('${baseUrl()}jaxrs/face/my/list');
      final list = response.data == null ? [] : response.data as List;
      return list.map((e) => FaceImageData.fromJson(e)).toList();
    } catch (err, stackTrace) {
      OLogger.e('myFaceList 查询失败', err, stackTrace);
    }
    return null;
  }

  /// 人脸比对 需要用户先注册人脸
  Future<ValueBoolData?> faceCompare(File file) async {
    try {
      ApiResponse response = await O2HttpClient.instance.postUploadFile('${baseUrl()}jaxrs/face/person/compare', file);
      return ValueBoolData.fromJson(response.data);
    } catch (err, stackTrace) {
      OLogger.e('faceCompare 失败', err, stackTrace);
    }
    return null;
  } 



}